﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AAA.Forms.Components.MetaData;

namespace AAA.Forms.Components
{

    public partial class RichTextLabel : UserControl
    {
        private List<LabelMeta> _lstText;
        private bool _isUpdateText;

        public RichTextLabel()
        {
            InitializeComponent();
            _lstText = new List<LabelMeta>();
            AutoSize = true;
            IsUpdateText = true;
        }

        public bool IsUpdateText
        {
            get { return _isUpdateText; }
            set 
            {
//                Visible = value;
                _isUpdateText = value;
            }
        }

        public void ClearText()
        {
            _lstText = new List<LabelMeta>();
            //Text = "";
        }

        public void AddText(LabelMeta labelMeta)
        {
            _lstText.Add(labelMeta);
        }

        private void PaintTitle(Graphics g)
        {
            string strTitle;
            int iStartX;
            int iStartY;
            SizeF size;
            int iWidth;

            try
            {

                if (_lstText.Count == 0)
                    return;
//                BackColor = Color.White;

                strTitle = "";
                foreach (LabelMeta labelMeta in _lstText)
                    strTitle += labelMeta.Text + "    ";
//                Text = strTitle;

                iWidth = 0;
                iStartX = 0;

                Width = (int)g.MeasureString(strTitle, Font).Width;
                Height = 5 + (int)g.MeasureString(strTitle, Font).Height;

                g.FillRectangle(new SolidBrush(BackColor), 0, 0, Width, Height);

                iStartY = (_lstText.Count > 0) ? (int)((Height - g.MeasureString(_lstText[0].Text, Font).Height) / 2.0) : 0;
               
                foreach (LabelMeta labelMeta in _lstText)
                {
                    size = g.MeasureString(labelMeta.Text + "    ", Font);
                    
                    g.DrawString(labelMeta.Text, Font, new SolidBrush(labelMeta.ForeColor), iStartX, iStartY);

                    iStartX += (int)size.Width;
                    iWidth += (int)size.Width;
                }

                //Width = iWidth;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "," + ex.StackTrace);
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {            
            if (IsUpdateText == false)
                return;
            base.OnPaint(e);
            PaintTitle(e.Graphics);
        }
    }
}
